<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org" style="height: 100%">
<head>
    <th:block th:insert="head/head :: common_head"></th:block>
    <link rel="stylesheet" th:href="@{/login/login.css?t=312}">
</head>
<body class="login-bg">
    <div class="login layui-anim layui-anim-up">
        <div class="message" th:text="${loginTitle}"></div>
        <form class="layui-form" autocomplete="off" name="login_form" id="login_form">
            <input id="sid" name="sid" type="hidden" value=""/>
            <input name="dlmc" id="dlmc" placeholder="用户名" class="layui-input" type="text" lay-verify="dlmc" autocomplete="off" onkeydown="on_key_tab('mm',event);">
            <hr class="hr15">
            <input name="mm" id="mm" lay-verify="mm" placeholder="密码" type="password" class="layui-input" onkeydown="on_key_tab('radomyzm',event);" autocomplete="off"/>
            <hr class="hr15">
            <table style="width:99%;">
                <tr>
                    <td>
                        <input id="radomyzm" name="radomyzm" type="text" lay-verify="required" class="layui-input" onkeydown="on_key_button('bc',event)" placeholder="验证码"/>
                    </td>
                    <td style="width: 3px"></td>
                    <td style="text-align: right;width: 40px;">
                        <img id="yzmCode" name="yzmCode" src="" onclick="reloadYzm();" style="height: 50px;">
                    </td>
                </tr>
            </table>
            <hr class="hr15">
            <input type="checkbox" id="jzyhm" name="jzyhm" lay-text="记住用户名" checked>
            <hr class="hr15">
            <input value="登录" lay-submit lay-filter="login" style="width:100%;" id="bc" type="button">
            <hr class="hr20" >
        </form>
    </div>
    <th:block th:insert="head/head :: common_jsfiles"></th:block>
    <script type="text/javascript" th:src="@{/jsfiles/login.js}" charset="utf-8"></script>
    <script>
        var sfqh="[[${sfqh}]]";
        var url="[[${serverUrl}]]";
        $(window).resize(function(){
            //窗口变化执行
        })
        $(document).ready(function() {
            //判断浏览器是否支持localStorage
            if (window.localStorage) {
            } else {
                alert('浏览器不支持存储数据，请更换高版本浏览器再进行登录操作！');
            }
            if (sfqh=="1"){
                init('dlmc');
                $("#dlmc").val("");
                $("#mm").val("")
            }else{
                let dlmc = localStorage.getItem("dlmc");
                $("#dlmc").val(dlmc);
                if ($("#dlmc").val()!=""){
                    init('mm')
                }else{
                    init('dlmc')
                }
            }
            reloadYzm();
        });
        function reloadYzm()
        {
            let loadIndex = layer.msg("验证码获取中...", {icon: 16, shade: 0.01, time: false});
            //验证码
            $.ajax({
                type:"post",
                data:'',
                url:url+"/vcode",
                timeout : 10*1000, //超时时间设置，单位毫秒
                crossDomain: true,
                success:function(result){
                    layer.close(loadIndex);
                    $("#yzmCode").attr("src","data:image/png;base64,"+result.yzm+"");
                    $("#sid").val(result.sid);
                },
                error:function(result){
                    layer.close(loadIndex);
                }
            });
        }
        function on_result(result)
        {
            $('#bc').removeClass("layui-btn-disabled").attr("disabled",false);
            if (result.code==200)
            {
                let rtn = JSON.parse(decrypt_str_fix(result.data));
                let token = rtn.token;
                let a = rtn.a;
                let b =rtn.b;
                let c =rtn.c;
                if (""==token || ""==a || ""==b || ""==c){
                    layer.msg("登录返回数据丢失！");
                }else{
                    //存储字段
                    localStorage.setItem("a",a);
                    localStorage.setItem("b",b);
                    localStorage.setItem("c",c);
                    localStorage.setItem("token",token);
                    if (sfqh=="1"){
                        window.parent.location.reload();
                    }else{
                        window.location.href="index";
                    }
                }
            }
            else if (result.code==10){
                layer.msg("验证码错误！", { time: 1000 }, function() {
                    $("#radomyzm").val("");
                    $("#mm").val("");
                    reloadYzm();
                    init('mm');
                });

            }
            else if (result.code==11){
                window.location.href="xgmm?sfqh="+sfqh+"&id="+result.data;
            }
            else
            {
                var msg="";
                if(is_null(result.msg))
                    msg = "登录发生错误！";
                else
                   msg = result.msg;

                layer.msg(msg, { time: 1000 }, function() {
                    $("#radomyzm").val("");
                    $("#mm").val("");
                    reloadYzm();
                    init('mm');
                });

            }
        }

        //表单相关
        layui.use(['form'], function(){
            var form = layui.form
                ,layer = layui.layer;

            //自定义验证规则
            form.verify({
                dlmc: function(value){
                    if(value==""){
                        init('dlmc')
                        return '请输入用户名！';
                    }
                },
                mm: function(value){
                    if(value==""){
                        init('mm')
                        return '请输入密码！';
                    }
                    let v1= encode64(value);
                    $("#mm").val(v1);
                }
            });

            //监听提交
            form.on('submit(login)', function(data){
                if (data.field.jzyhm=="on"){
                    localStorage.setItem("dlmc",data.field.dlmc);
                }else{
                    localStorage.setItem("dlmc","");
                }
                $('#bc').addClass("layui-btn-disabled").attr("disabled",true);
                let json = JSON.stringify(data.field);
                console.info("json:"+json);
                post_json("[[${serverUrl}]]/loginDo",json,"登录中...",on_result);
                return false;
            });
        });

    </script>
</body>
</html>
